# =============================================================================
# FILE 0. PACKAGES / DEPENDENCIES
# =============================================================================
# Purpose:
#   Centralized package loading for this project, grouped by function.
#
# Notes:
#   - `library()` calls assume packages are already installed.
#   - If replicating on a new machine, install any missing packages first.
#     Example: install.packages(c("tidyverse","haven","janitor", ...))
#   - `pacman::p_load()` will install missing packages automatically (if desired).
# =============================================================================

# ---- Console formatting / numeric printing ----------------------------------
options(scipen = 999)  # Avoid scientific notation (e.g., 1e+05 -> 100000)

# ---- Package manager helper -------------------------------------------------
# Installs (if missing) + loads the package(s) listed.
pacman::p_load(lmerTest)  # Mixed-effects models with p-values / tests (extends lme4)

# ---- Core data handling (tidy workflow) -------------------------------------
library(dplyr)       # Data manipulation (filter, mutate, summarize, joins)
library(tidyr)       # Data reshaping (pivot_longer, pivot_wider, separate, unite)
library(tidyverse)   # Meta-package (ggplot2, dplyr, tidyr, readr, purrr, tibble, etc.)

# ---- Data import / export / labelled survey support --------------------------
library(haven)       # Read/write SPSS, Stata, SAS (tibble-friendly)
library(foreign)     # Legacy import/export for SPSS/Stata/etc. (older but sometimes needed)
library(readstata13) # Read Stata v13+ .dta files
library(labelled)    # Work with labelled variables (common in survey microdata)
library(writexl)     # Write Excel .xlsx files

# ---- Plotting (ggplot + extensions / layout) --------------------------------
library(ggplot2)       # Grammar of Graphics plotting system
library(ggpubr)        # Publication-ready ggplot helpers (themes, stats layers)
library(ggstatsplot)   # Statistical annotations + plots (tests + visuals)
library(scales)        # Axis/scale formatting (percent, comma, currency, etc.)
library(grid)          # Low-level grid graphics (layout control for ggplot grobs)
library(ggparliament)  # Parliamentary seat charts / election visualization
library(corrplot)      # Correlation matrix visualization

# ---- Image I/O / processing -------------------------------------------------
library(png)      # Read/write PNG images (useful for plot compositing / exports)
library(magick)   # Image processing via ImageMagick (crop, annotate, compose)

# ---- Missing data / cleaning helpers ----------------------------------------
library(janitor)  # Clean names, quick tabulations, simple cleaning utilities
library(naniar)   # Missing data summaries + visualization

# ---- Modeling / post-estimation / effects -----------------------------------
library(effects)    # Visualize model effects (e.g., regression effects)
library(ggeffects)  # Tidy marginal effects / predictions for ggplot workflows
library(emmeans)    # Estimated marginal means / contrasts (post hoc comparisons)
library(sjPlot)     # Model tables / plots (common in applied social science)
library(sjmisc)     # Data transformation + labeling helpers (often paired with sjPlot)
library(nnet)       # Multinomial logit and related models (plus neural nets utilities)
library(ordinal)    # Ordinal regression models (cumulative link models)

# ---- Factor analysis / SEM / psychometrics ----------------------------------
library(psych)           # Psychometrics: FA, reliability, descriptive tools
library(EFAtools)        # Exploratory factor analysis helpers
library(EFA.dimensions)  # Dimensionality / factor retention utilities
library(lavaan)          # SEM / CFA framework
library(semTools)        # SEM utilities (e.g., measurement invariance)

# ---- Statistical utilities / metrics ----------------------------------------
library(boot)     # Bootstrap resampling tools
library(MASS)     # Common statistical functions / datasets (e.g., stepAIC)
library(Metrics)  # Model evaluation metrics (MAE, RMSE, etc.)
library(lm.beta)  # Standardized beta coefficients for linear models

# ---- Distribution comparison / misc. utilities ------------------------------
library(overlapping)  # Visualize/compare overlapping distributions
library(misty)        # Misc. multivariate / social-science utility functions (project-specific)

# ---- Legacy / compatibility --------------------------------------------------
library(plyr)  # Older split-apply-combine; can conflict with dplyr (load only if needed)

# ---- Locale (optional; only if you need consistent month labels) ------------
# Ensures month names / date labels render in English consistently across machines.
Sys.setlocale("LC_TIME", "en_US.UTF-8")